﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SerializedData;
using KnowledgeBaseIntermediary;

namespace KnowledgeBaseManager
{
    public partial class MainWindow : Form
    {
        private DataCollection mDataCollection = DataCollection.Instance;

        public MainWindow()
        {
            InitializeComponent();
            
            refreshCommandList();
            refreshSceneList();
            refreshStateList();
            refreshSensorList();
        }

        public void refreshCommandList()
        {
            listViewCommands.Clear();
            listViewCommands.BeginUpdate();
            listViewCommands.Columns.Add("CID", 50);
            listViewCommands.Columns.Add("Name", 200);
            listViewCommands.Columns.Add("Description", 300);
            listViewCommands.Columns.Add("TTS", 300);
            foreach (SCommand command in mDataCollection.getCommandPool())
            {
                ListViewItem oneRow = listViewCommands.Items.Add(""+command.getId());
                oneRow.SubItems.Add(command.getName());
                oneRow.SubItems.Add(command.getDescription());
                oneRow.SubItems.Add(command.getTts());

            }
            listViewCommands.EndUpdate();
        }

        public void refreshStateList()
        {
            listViewStates.Clear();
            listViewStates.BeginUpdate();
            listViewStates.Columns.Add("SID", 50);
            listViewStates.Columns.Add("Checked", 60);
            listViewStates.Columns.Add("Priority", 80);
            listViewStates.Columns.Add("Name", 150);
            listViewStates.Columns.Add("Description", 300);
            listViewStates.Columns.Add("TTS", 200);

            foreach (SState state in mDataCollection.getStatePool())
            {
                ListViewItem oneRow = listViewStates.Items.Add("" + state.getId());
                oneRow.SubItems.Add(""+state.isChecked());
                oneRow.SubItems.Add(state.getPriority().ToString());
                oneRow.SubItems.Add(state.getName());
                oneRow.SubItems.Add(state.getDescription());
                oneRow.SubItems.Add(state.getTts());

            }
            listViewStates.EndUpdate();
        }

        public void refreshSceneList()
        {
            listViewScenes.Clear();
            listViewScenes.BeginUpdate();
            listViewScenes.Columns.Add("SID", 50);
            listViewScenes.Columns.Add("Name", 200);
            listViewScenes.Columns.Add("Description", 300);
            listViewScenes.Columns.Add("TTS", 300);
            foreach (SScene scene in mDataCollection.getScenePool())
            {
                ListViewItem oneRow = listViewScenes.Items.Add(""+scene.getId());
                oneRow.SubItems.Add(scene.getName());
                oneRow.SubItems.Add(scene.getDescription());
                oneRow.SubItems.Add(scene.getTts());
            }
            listViewScenes.EndUpdate();
        }

        public void refreshSensorList()
        {
            listViewSensors.Clear();
            listViewSensors.BeginUpdate();
            listViewSensors.Columns.Add("SID", 50);
            listViewSensors.Columns.Add("Name", 200);
            listViewSensors.Columns.Add("UDP", 300);
            listViewSensors.Columns.Add("Action", 300);
            foreach (SSensor sensor in mDataCollection.getSensorPool())
            {
                ListViewItem oneRow = listViewSensors.Items.Add(""+sensor.getId());
                oneRow.SubItems.Add(sensor.getName());
                oneRow.SubItems.Add(sensor.getUdp());
                if(sensor.getRefState()==-1) {
                    oneRow.SubItems.Add("");
                } else {
                    SState state=mDataCollection.getStatePool().Find(o => o.getId().Equals(sensor.getRefState()));
                    oneRow.SubItems.Add("Call State "+state.getName());
                }
                
            }
            listViewSensors.EndUpdate();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void listViewCommands_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void buttonNewCommand_Click(object sender, EventArgs e)
        {
            CreateCommand cc = new CreateCommand();
            cc.Owner = this;
            cc.ShowDialog();
            
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            mDataCollection.saveChanges();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            mDataCollection.saveChanges();
            this.Close();
        }

        private void buttonRemoveCommand_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection selCol = listViewCommands.SelectedItems;
            foreach (ListViewItem item in selCol)
            {
                listViewCommands.Items.Remove(item);
                mDataCollection.removeCommandById(item.Text);
            }
            refreshCommandList();
        }

        private void buttonNewScene_Click(object sender, EventArgs e)
        {

            CreateScene cs = new CreateScene(new SScene(mDataCollection.getSceneInt(),"", "", ""));
            cs.Owner = this;
            cs.ShowDialog();
        }

        private void buttonModifyScene_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection selCol = listViewScenes.SelectedItems;
            if (selCol.Count > 0)
            {
                SScene sScene = mDataCollection.getScenePool().Find(o => ("" + o.getId()).Equals(selCol[0].Text));
                if (sScene != null)
                {
                    CreateScene cs = new CreateScene(sScene);
                    cs.Owner = this;
                    cs.ShowDialog();
                }
            }
        }

        private void buttonRemoveScene_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection selCol = listViewScenes.SelectedItems;
            foreach (ListViewItem item in selCol)
            {
                listViewScenes.Items.Remove(item);
                mDataCollection.removeSceneById(item.Text);
            }
            refreshCommandList();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            CState cs = new CState(new SState(mDataCollection.getStateInt(),"","","",false,StateDTO.PRIORITY.MEDIUM));
            cs.Owner = this;
            cs.ShowDialog();
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection selCol = listViewStates.SelectedItems;
            if (selCol.Count > 0)
            {
                SState sState = mDataCollection.getStatePool().Find(o => ("" + o.getId()).Equals(selCol[0].Text));
                if (sState != null)
                {
                    CState cs = new CState(sState);
                    cs.Owner = this;
                    cs.ShowDialog();
                }
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection selCol = listViewStates.SelectedItems;
            foreach (ListViewItem item in selCol)
            {
                listViewStates.Items.Remove(item);
                mDataCollection.removeStateById(item.Text);
            }
            refreshCommandList();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            AddSensor cs = new AddSensor(new SSensor(mDataCollection.getSensorInt(),"","",""));
            cs.Owner = this;
            cs.ShowDialog();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection selCol = listViewSensors.SelectedItems;
            foreach (ListViewItem item in selCol)
            {
                listViewSensors.Items.Remove(item);
                mDataCollection.removeSensorById(item.Text);
            }
            refreshSensorList();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection selCol = listViewSensors.SelectedItems;
            if (selCol.Count > 0)
            {
                SSensor sSensor = mDataCollection.getSensorPool().Find(o => ("" + o.getId()).Equals(selCol[0].Text));
                if (sSensor != null)
                {
                     AddSensor cs = new AddSensor(sSensor);
                    cs.Owner = this;
                    cs.ShowDialog();
                }
            }

        }


     

    }
}
